package com.wbs.rest.handler;

import com.wbs.rest.config.SqlConst;
import com.wbs.rest.model.MultiSqlModel;
import com.wbs.rest.model.SqlModel;
import com.wbs.rest.model.SqlModelEnum;
import java.text.SimpleDateFormat;
import java.util.Date;



public class AutoGenerateModelHandler implements SqlModelHandler {

    @Override
    public void handle(SqlModel sqlModel, SqlModelEnum modelEnum) {
        if(sqlModel.getArgNames() == null || sqlModel.getArgNames().size() <= 0){
            return;
        }

        int i = sqlModel.getArgNames().indexOf(SqlConst.AUTO_DATETIME);
        if(i >=0 && i < sqlModel.getArgs().length){
            sqlModel.getArgs()[i] = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        }

        int j = sqlModel.getArgNames().indexOf(SqlConst.AUTO_DATE);
        if(j >=0 && j < sqlModel.getArgs().length){
            sqlModel.getArgs()[j] = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }

        if(modelEnum == SqlModelEnum.MULTI){
            for (SqlModel model : ((MultiSqlModel)sqlModel).getSqlModels()){
                handle(model, null);
            }
        }

    }

}
